package net.jumpengine.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

/**
 * 音乐管理类
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020-02-19 13:11<br />
 * @date
 */
public class MusicManager {
	private Map<String,MediaPlayer> map=new HashMap<>();
	private List<MediaPlayer> autoPlayList=new ArrayList<MediaPlayer>();
	
	public MusicManager() {}
	
	public void load(Properties pro) {
		for(Entry<Object,Object> e:pro.entrySet()) {
			map.put(e.getKey().toString(), new MediaPlayer(new Media(e.getValue().toString())));
		}
	}
	
	public void clearAndLoad(Properties pro) {
		map.clear();
		load(pro);
	}
	
	public MediaPlayer get(String name) {
		return get(name,false);
	}
	
	public MediaPlayer get(String name,boolean autoPlay) {
		MediaPlayer mediaPlayer = map.get(name);
		mediaPlayer.setAutoPlay(autoPlay);
		if(autoPlay)autoPlayList.add(mediaPlayer);
		return mediaPlayer;
	}
	
	public void stop() {
		for(MediaPlayer m:autoPlayList) {
			m.stop();
		}
	}
	
	public void start() {
		for(MediaPlayer m:autoPlayList) {
			m.play();
		}
	}
	
	public void stop(String name) {
		MediaPlayer mediaPlayer = map.get(name);
		if(mediaPlayer!=null)mediaPlayer.stop();
	}
	
	public void play(String name) {
		MediaPlayer mediaPlayer = map.get(name);
		if(mediaPlayer!=null)mediaPlayer.play();
	}
}
